package business;
import com.shove.Convert;
import models.*;
import org.apache.axis.utils.StringUtils;
import org.apache.poi.util.StringUtil;
import play.Logger;
import play.db.jpa.JPA;
import play.mvc.Scope;
import utils.*;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;

public class CustomerInfo{
    public static PageBean<customer_info> select(StringBuilder sbSql, StringBuilder sbSqlCnt, Scope.Params params) {
        int currPage = Constants.ONE;
        int pageSize = Constants.PAGE_SIZE;
        String currPageStr = params.get(Constants.KEY_CURR_PAGE); // 当前页
        String pageSizeStr = params.get(Constants.KEY_PAGE_SIZE); // 分页行数
        if (NumberUtil.isNumericInt(currPageStr)) {
            currPage = Integer.parseInt(currPageStr);
        }
        if (NumberUtil.isNumericInt(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        try {
            EntityManager em = JPA.em();
            Query query = em.createNativeQuery(sbSql.toString(), customer_info.class);
            query.setFirstResult((currPage - 1) * pageSize);
            query.setMaxResults(pageSize);
            List<customer_info> customerInfoList = query.getResultList();
            String countStr = JPAUtil.createNativeQuery(sbSqlCnt.toString()).getSingleResult().toString();
            int count = Convert.strToInt(countStr,0);
            PageBean<customer_info> page = new PageBean();
            page.pageSize = pageSize;
            page.currPage = currPage;
            page.totalCount = count;
            page.page = customerInfoList;
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            Logger.error("BaseBiz.queryList.error:" + e.getMessage());
            return null;
        }
    }
    public static ErrorInfo add(Scope.Params params) {
        customer_info customerInfo;
        String id = params.get("id");
        if(!StringUtils.isEmpty(id)){
            long idL = Long.parseLong(id);
            customerInfo = customer_info.findById(idL);
        }else{
             customerInfo = new customer_info();
            customerInfo.create_date = new Date();
        }
        customerInfo.business_id = SysUser.getSessionUser().getId();
        customerInfo.customer_name = params.get("customer_name");
        customerInfo.filename = params.get("filename");
        customerInfo.pay_status = Integer.parseInt(params.get("pay_status"));
        customerInfo.buy_date =DateUtil.strToDate(params.get("buy_date"));
        customerInfo.sum_price =Convert.strToDouble(params.get("sum_price"),0);
        customerInfo.all_count = Convert.strToInt(params.get("all_count"),0);
        customerInfo.single_price = Convert.strToDouble(params.get("single_price"),0);
        customerInfo.pay_status = Convert.strToInt(params.get("pay_status"),0);
        customerInfo.common = params.get("common");
        customerInfo.customer_level = params.get("customer_level");
        customerInfo.customer_source = params.get("customer_source");
        customerInfo.save();
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.initSave();
        return errorInfo;
    }
}
